<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/BlogAdmin/layui/css/layui.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend style="text-align: center">iArchie博客</legend>
</fieldset>
<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">资源管理信息</div>
                    <div class="layui-card-body">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">资源分类:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="resourcetype" placeholder="请输入资源分类"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">资源描述:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="resourcecontent" placeholder="请输入资源描述"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">资源图片链接:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="resourceimg" placeholder="请输入资源图片链接"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">资源链接:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="resourcelink" placeholder="请输入资源链接"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">资源标题:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="resourcetitle" placeholder="请输入资源标题"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">提供资源作者:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="resourceauthor" placeholder="请输入提供资源作者"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <button id="addResourceBtn" class="layui-btn layui-btn-warm layui-btn-radius">点击添加</button>
                        </div>
                        <table id="demo" lay-filter="test"></table>
                    </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/BlogAdmin/layui/layui.js"></script>
<script src="/LayuiBlog/js/jquery-min.js"></script>
<script>
    $("#addResourceBtn").click(function () {
        var resourcetype=$("[name='resourcetype']").val();
        var resourcecontent=$("[name='resourcecontent']").val();
        var resourceimg=$("[name='resourceimg']").val();
        var resourcelink=$("[name='resourcelink']").val();
        var resourcetitle=$("[name='resourcetitle']").val();
        var resourceauthor=$("[name='resourceauthor']").val();
        var newdata={
            "resourcetype":resourcetype,
            "resourcecontent":resourcecontent,
            "resoureimg":resourceimg,
            "resourcelink":resourcelink,
            "resourcetitle":resourcetitle,
            "resourceauthor":resourceauthor
        }
        $.ajax( {
            url:"/admin/addResource",
            dataType:"json",
            type:"POST",
            contentType : 'application/json',
            data :JSON.stringify(newdata),
            success: function (res) {
                if (res.result == 1) {
                    window.location.reload();
                    layer.msg("增加资源成功！", {icon: 6});
                    layer.close(index);
                } else {
                    layer.msg("增加资源失败！", {icon: 5});
                }
            }
        })
    })
</script>
<script>
    layui.use('table', function () {
        var table = layui.table;

        //第一个实例
        table.render({
            elem: '#demo'
            , height: 600
            , url: '/admin/getResourceListMang'
            , page: true
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            , request: {
                pageName: 'pageNum', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , cols: [
                        [
                {type: 'checkbox', fixed: 'left'}
                , {field: 'resourceid', title: '资源ID', width: 90, sort: true}
                , {field: 'resourcetype', title: '资源分类', width: 130, sort: true}
                , {field: 'resourcecontent', title: '资源描述', width: 150}
                , {field: 'resoureimg', title: '资源图片链接', width: 230}
                , {field: 'resourcelink', title: '资源链接', width: 230}
                , {field: 'resourcetitle', title: '资源标题', width: 150, sort: true}
                , {field: 'resourceauthor', title: '提供资源作者', width: 150}
                , {
                    fixed: 'right',
                    title: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作',
                    toolbar: '#barDemo',
                    width: 180
                }
                    ]
                ]
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除ID为：'+data.resourceid+"的资源吗？", function (index) {
                    $.ajax({
                        type: 'post',
                        url: "/admin/deleteResourceByResourceId",
                        // contentType: "application/x-www-form-urlencoded",
                        dataType: "json",
                        data:{
                            resourceId:data.resourceid
                        },
                        success: function (res) {
                            if (res.result == 1) {
                                layer.msg("删除资源成功！", {icon: 6});
                            } else {
                                layer.msg("删除资源失败！", {icon: 5});
                            }
                        },
                        error: function () {
                            layer.msg("删除资源失败！", {icon: 5});
                        }
                    })
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    area: ['45%', '500px'],
                    title: '编辑评论',
                    skin: 'layui-layer-rim', //加上边框
                    btn: ['保存', '取消'],
                    btnAlign: 'c',
                    top: 100,
                    content: ['/admin/layer_model5', 'no'],
                    // content:$("#popUpdateTest") //引用的弹出层的页面层的方式加载修改界面表单
                    success: function (layero,index) {
                        var body=layui.layer.getChildFrame('body',index);
                        body.find("[name=resourceId]").val(data.resourceid);
                        body.find("[name=resourcetype]").val(data.resourcetype);
                        body.find("[name=resourcecontent]").val(data.resourcecontent);
                        body.find("[name=resourceimg]").val(data.resoureimg);
                        body.find("[name=resourcelink]").val(data.resourcelink);
                        body.find("[name=resourcetitle]").val(data.resourcetitle);
                        body.find("[name=resourceauthor]").val(data.resourceauthor);
                    },
                    yes:function (index) {
                        var body=layui.layer.getChildFrame('body',index);
                        var resourcetype=body.find("[name=resourcetype]").val();
                        var resourcecontent=body.find("[name=resourcecontent]").val();
                        var resourceimg=body.find("[name=resourceimg]").val();
                        var resourcelink=body.find("[name=resourcelink]").val();
                        var resourcetitle=body.find("[name=resourcetitle]").val();
                        var resourceauthor=body.find("[name=resourceauthor]").val();
                        var resourceId=data.resourceid;
                        var newdata={
                            "resourceid":resourceId,
                            "resourcetype":resourcetype,
                            "resourcecontent":resourcecontent,
                            "resoureimg":resourceimg,
                            "resourcelink":resourcelink,
                            "resourcetitle":resourcetitle,
                            "resourceauthor":resourceauthor
                        }
                        $.ajax( {
                            url:"/admin/updateResourceByResourceId",
                            dataType:"json",
                            type:"POST",
                            contentType : 'application/json',
                            data :JSON.stringify(newdata),
                            // data:{
                            //     resourceId:resourceId,
                            //     resourcetype:resourcetype,
                            //     resourcecontent:resourcecontent,
                            //     resourceimg:resourceimg,
                            //     resourcelink:resourcelink,
                            //     resourcetitle:resourcetitle,
                            //     resourceauthor:resourceauthor,
                            // },
                            success: function (res) {
                                if (res.result == 1) {
                                    obj.update({
                                        resourceId:resourceId,
                                        resourcetype:resourcetype,
                                        resourcecontent:resourcecontent,
                                        resourceimg:resourceimg,
                                        resourcelink:resourcelink,
                                        resourcetitle:resourcetitle,
                                        resourceauthor:resourceauthor,
                                    });
                                    layer.msg("更新资源成功！", {icon: 6});
                                    layer.close(index);
                                } else {
                                    layer.msg("更新资源失败！", {icon: 5});
                                }
                            }
                        })
                    }
                });
            }
        });
    });

</script>
</body>
</html>